import 'dart:async';
import 'package:firn/FirnClient.dart';
import 'package:firn/datatypes/FirnConfig.dart';
import 'package:firn/events/MessageRecievedEvent.dart';
import 'utils.dart';

class IrcClient{
  static String channel="#diugou_vod";
         String host="irc.freenode.net";
         int port=6667;
         String uuid="";
  void connect(){
    Utils.getUniqueId().then((uuid){
      uuid=uuid;
      /// the main IRC client
      FirnClient TestClient = new FirnClient();

      /// a configuration to connect to a server,
      /// the FirnClient class can handle many configurations
      /// see pool.dart
      FirnConfig TestConfig = new FirnConfig();

      TestConfig.server = host;
      TestConfig.nickname = "diugou_$uuid";
      TestConfig.realname = "diugou_$uuid";
      TestConfig.port = 6667;

      TestClient.addConfig(TestConfig);


      /// 这个 globalEventController 监听所有事件,
      /// 不管他们来自哪个服务器
      TestClient.globalEventController.stream.listen((event) async {
        /// 事件的类型为FirEvent
        /// FirEvent是基类
        /// 只包含名称和 FirnConfig
        if (event.eventName == "ready") {
          print("Ready, joining channel");
          /// 加入频道
          TestClient.joinChannel(event.config, channel);
        }
      });


      TestClient.globalEventController.stream.listen((event) {
        /// FirnEvent.eventName is used to determine the
        /// 传入事件的类型
        if (event.eventName == "privMsgRecieved") {
          MessageRecievedEvent msg = event; /// MessageRecievedEvent is an extension of FirnEvent
          /// msg.message is of type Message, check /lib/datatypes/IRCMessage.dart
          String target = msg.message.parameters[0];
          String message = msg.message.parameters[1];
          print(target);
          print(message);
          if (message.startsWith(event.config.nickname)) {
            TestClient.sendPrivMsg(event.config, target, "Hello!");
          }
        }
      });
      TestClient.connectToServers();
    });

  }

}
